<?php

if (!defined('IN_AJAX')) die(basename(__FILE__));

global $bb_cfg, $lang, $userdata, $datastore, $tr_cfg;

$mode = (string) $this->request['mode'];
$html = '';

switch($mode)
{	
	case 'torr':
		$user_id = (int) $this->request['user_id'];
		$profiledata = get_userdata($user_id);
		
		$seeding = $leeching = $releasing = array();

		$sql = "SELECT f.forum_id, f.forum_name, t.topic_title, sn.seeders, sn.leechers, tor.size, tor.tor_type, tor.tor_status, tr.*
			FROM ". BB_FORUMS ." f, ". BB_TOPICS ." t, ". BB_BT_TRACKER ." tr, ". BB_BT_TORRENTS ." tor
			LEFT JOIN ". BB_BT_TRACKER_SNAP ." sn ON(tor.topic_id = sn.topic_id)
			WHERE tr.user_id = $user_id
				AND tr.topic_id = tor.topic_id
				AND tor.topic_id = t.topic_id
				AND t.forum_id = f.forum_id
			GROUP BY tr.topic_id
			ORDER BY f.forum_name, t.topic_title";
			
		if (!$result = DB()->sql_query($sql))
		{
			message_die(GENERAL_ERROR, 'Could not query users torrent profile information', '', __LINE__, __FILE__, $sql);
		}
			
			if ($rowset = @DB()->sql_fetchrowset($result))
			{
				DB()->sql_freeresult($result);
				$rowset_count = count($rowset);

				for ($i=0; $i<$rowset_count; $i++)
				{
					if ($rowset[$i]['releaser'] and $rowset[$i]['seeder'])
					{
						$releasing[] = $rowset[$i];
					}
					else if ($rowset[$i]['seeder'])
					{
						$seeding[] = $rowset[$i];
					}
					else
					{
						$leeching[] = $rowset[$i];
					}
				}
				unset($rowset);
			}
			
			$html .= '
			<table class="forumline tablesorter bordered w100" id="dls-tbl">
			<thead>
			<tr>
				<th class="{sorter: "digit"}"><b class="tbs-text">'. $lang['ACTIVE_TYPE'] .'</b></th>
				<th class="{sorter: "text"}" width="25%"><b class="tbs-text">'. $lang['FORUM'] .'</b></th>
				<th class="{sorter: "text"}" width="75%"><b class="tbs-text">'. $lang['TOPIC'] .'</b></th>
				<th class="{sorter: false}">'. $lang['ACTIVE_TORRENTS'] .'</th>
			</tr>
			</thead>
			<tbody>
			';
				$releasing_count = count($releasing);
				$releas_size = 0;	
				for ($i=0; $i<$releasing_count; $i++)
				{
					$releas_size = $releas_size + $releasing[$i]['size'];
					$tor_status_icon  = isset($releasing[$i]['tor_status']) ? $bb_cfg['tor_icons'][$releasing[$i]['tor_status']] : '';
					$tor_status_text  = isset($releasing[$i]['tor_status']) ? $lang['TOR_STATUS_NAME'][$releasing[$i]['tor_status']] : '';			
					$seeders      = isset($releasing[$i]['seeders']) ? $releasing[$i]['seeders'] : '0';
					$leechers     = isset($releasing[$i]['leechers'])? $releasing[$i]['leechers'] : '0 ';
					$u_view_forum = "viewforum.php?". POST_FORUM_URL .'='. $releasing[$i]['forum_id'];
					$u_view_topic = "viewtopic.php?". POST_TOPIC_URL .'='. $releasing[$i]['topic_id'] .'&amp;spmode=full#seeders';
					$is_gold = '';
					if ($tr_cfg['gold_silver_enabled'])
					{
						if ($releasing[$i]['tor_type'] == TOR_TYPE_GOLD)
						{
							$is_gold = '<img src="images/tor_gold.gif" width="16" height="15" title="'.$lang['GOLD'].'" />&nbsp;';
						}
						elseif ($releasing[$i]['tor_type'] == TOR_TYPE_SILVER)
						{
							$is_gold = '<img src="images/tor_silver.gif" width="16" height="15" title="'.$lang['SILVER'].'" />&nbsp;';
						}
					}
					$html .= '
					<tr class="row1">
						<td class="dls-type tCenter"><u>0</u><b class="seedmed">'. $lang['RELEASING'] .'</b></td>
						<td class="dls-f tCenter"><a class="gen" href="'.$u_view_forum.'">'.htmlCHR($releasing[$i]['forum_name']).'</a></td>
						<td class="pad_4"><span title="'.$tor_status_text.'">'.$tor_status_icon.'</span>&nbsp;&#0183;&nbsp;<a class="med" href="'.$u_view_topic.'">'.$is_gold.'<b>'.wbr($releasing[$i]['topic_title']).'</b></a></td>
						<td class="tCenter med nowrap pad_2">
							<div>
								<p><b class="seedmed">'.$seeders.'</b> | <b class="leechmed">'.$leechers.'</b></p>
								<p style="padding-top: 2px" class="seedsmall">0 KB/s</p>
							</div>
						</td>
					</tr>
					';
				}
						
			$seeding_count = count($seeding);
			
				for ($i=0; $i<$seeding_count; $i++)
				{
					$tor_status_icon  = isset($seeding[$i]['tor_status']) ? $bb_cfg['tor_icons'][$seeding[$i]['tor_status']] : '';
					$tor_status_text  = isset($seeding[$i]['tor_status']) ? $lang['TOR_STATUS_NAME'][$seeding[$i]['tor_status']] : '';			
					$seeders      = isset($seeding[$i]['seeders']) ? $seeding[$i]['seeders'] : '0';
					$leechers     = isset($seeding[$i]['leechers'])? $seeding[$i]['leechers'] : '0 ';
					$u_view_forum = "viewforum.php?". POST_FORUM_URL .'='. $seeding[$i]['forum_id'];
					$u_view_topic = "viewtopic.php?". POST_TOPIC_URL .'='. $seeding[$i]['topic_id'] .'&amp;spmode=full#seeders';
					$is_gold = '';
					if ($tr_cfg['gold_silver_enabled'])
					{
						if ($seeding[$i]['tor_type'] == TOR_TYPE_GOLD)
						{
							$is_gold = '<img src="images/tor_gold.gif" width="16" height="15" title="'.$lang['GOLD'].'" />&nbsp;';
						}
						elseif ($seeding[$i]['tor_type'] == TOR_TYPE_SILVER)
						{
							$is_gold = '<img src="images/tor_silver.gif" width="16" height="15" title="'.$lang['SILVER'].'" />&nbsp;';
						}
					}
					$html .= '
					<tr class="row1">
						<td class="dls-type tCenter"><u>1</u><b class="seedmed">'. $lang['SEEDING'] .'</b></td>
						<td class="dls-f tCenter"><a class="gen" href="'.$u_view_forum.'">'.htmlCHR($seeding[$i]['forum_name']).'</a></td>
						<td class="pad_4"><span title="'.$tor_status_text.'">'.$tor_status_icon.'</span>&nbsp;&#0183;&nbsp;<a class="med" href="'.$u_view_topic.'">'.$is_gold.'<b>'.wbr($seeding[$i]['topic_title']).'</b></a></td>
						<td class="tCenter med nowrap pad_2">
							<div>
								<p><b class="seedmed">'.$seeders.'</b> | <b class="leechmed">'.$leechers.'</b></p>
								<p style="padding-top: 2px" class="seedsmall">0 KB/s</p>
							</div>
						</td>
					</tr>
					';
				}
						
			$leeching_count = count($leeching);
			
				for ($i=0; $i<$leeching_count; $i++)
				{
					$tor_status_icon  = isset($leeching[$i]['tor_status']) ? $bb_cfg['tor_icons'][$leeching[$i]['tor_status']] : '';
					$tor_status_text  = isset($leeching[$i]['tor_status']) ? $lang['TOR_STATUS_NAME'][$leeching[$i]['tor_status']] : '';			
					$seeders      = isset($leeching[$i]['seeders']) ? $leeching[$i]['seeders'] : '0';
					$leechers     = isset($leeching[$i]['leechers'])? $leeching[$i]['leechers'] : '0 ';
					$u_view_forum = "viewforum.php?". POST_FORUM_URL .'='. $leeching[$i]['forum_id'];
					$u_view_topic = "viewtopic.php?". POST_TOPIC_URL .'='. $leeching[$i]['topic_id'] .'&amp;spmode=full#seeders';
					$is_gold = '';
					if ($tr_cfg['gold_silver_enabled'])
					{
						if ($leeching[$i]['tor_type'] == TOR_TYPE_GOLD)
						{
							$is_gold = '<img src="images/tor_gold.gif" width="16" height="15" title="'.$lang['GOLD'].'" />&nbsp;';
						}
						elseif ($leeching[$i]['tor_type'] == TOR_TYPE_SILVER)
						{
							$is_gold = '<img src="images/tor_silver.gif" width="16" height="15" title="'.$lang['SILVER'].'" />&nbsp;';
						}
					}
					$html .= '
					<tr class="row1">
						<td class="dls-type tCenter"><u>2</u><b class="leechmed">'. $lang['LEECHING'] .'</b></td>
						<td class="dls-f tCenter"><a class="gen" href="'.$u_view_forum.'">'.htmlCHR($leeching[$i]['forum_name']).'</a></td>
						<td class="pad_4"><span title="'.$tor_status_text.'">'.$tor_status_icon.'</span>&nbsp;&#0183;&nbsp;<a class="med" href="'.$u_view_topic.'">'.$is_gold.'<b>'.wbr($leeching[$i]['topic_title']).'</b></a></td>
						<td class="tCenter med nowrap pad_2">
							<div>
								<p><b class="seedmed">'.$seeders.'</b> | <b class="leechmed">'.$leechers.'</b></p>
								<p style="padding-top: 2px" class="seedsmall">0 KB/s</p>
							</div>
						</td>
					</tr>
					';
				}
			
		$html .= '</table>'; 
	break;
}
$this->response['html']	= $html;
$this->response['mode']	= $mode;
